package com.huyi.common.config;

import java.util.HashMap;
import java.util.Map;

import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;

/**
 * kafka生产者配置
 * 
 * @author shengli.hu
 *
 */
@Configuration
@EnableKafka
public class KafkaProducerConfig {

	private Logger log = LoggerFactory.getLogger(KafkaProducerConfig.class);
	
	// kakfa服务端地址
	@Value("${kafka.producer.servers}")
	private String servers;

	// 消息发送失败重试次数
	@Value("${kafka.producer.retries}")
	private int retries;

	// 消息批量发送容量
	@Value("${kafka.producer.batch.size}")
	private int batchSize;

	// 往kafka服务器提交消息间隔时间，0则立即提交不等待
	@Value("${kafka.producer.linger}")
	private int linger;

	// 缓存容量
	@Value("${kafka.producer.buffer.memory}")
	private int bufferMemory;

	/**
	 * 生产者相关配置
	 * 
	 * @return
	 */
	public Map<String, Object> producerConfigs() {
		log.info("kafaka配置start...");
		Map<String, Object> props = new HashMap<>();
		props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
		props.put(ProducerConfig.RETRIES_CONFIG, retries);
		props.put(ProducerConfig.BATCH_SIZE_CONFIG, batchSize);
		props.put(ProducerConfig.LINGER_MS_CONFIG, linger);
		props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, bufferMemory);
		props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
		props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
		return props;
	}

	/**
	 * 生产者创建工厂
	 * 
	 * @return
	 */
	public ProducerFactory<String, String> producerFactory() {
		return new DefaultKafkaProducerFactory<>(producerConfigs());
	}

	/**
	 * kafkaTemplate 覆盖默认配置类中的kafkaTemplate
	 * 
	 * @return
	 */
	@Bean
	public KafkaTemplate<String, String> kafkaTemplate() {
		return new KafkaTemplate<String, String>(producerFactory());
	}
}
